package com.slkj.shilixiaoyuanapp.ui.main;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.flyco.tablayout.SlidingTabLayout;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.BaseLazyFragment;
import com.slkj.shilixiaoyuanapp.ui.main.community.SendCircleActivity;
import com.slkj.shilixiaoyuanapp.ui.main.community.CommunityFragment;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created  on 2018-07-15
 *
 * @author 唐开阔
 * @describe 首页社区fragment
 */
public class FindFragment extends BaseLazyFragment implements View.OnClickListener {

    @BindView(R.id.tab_layout)
    SlidingTabLayout tabLayout;
    @BindView(R.id.viewpager)
    ViewPager viewPager;
    @BindView(R.id.iv_take_picture)
    ImageView picture;
    private MyPagerAdapter adapter;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private final String[] mTitles = {"社区", "朋友圈"};

    @Override
    protected int getResId() {
        return R.layout.fragment_find;
    }

    @Override
    protected void onRealLoaded() {
        //咨询
        mFragments.add(new CommunityFragment());
        //朋友圈
//        mFragments.add(new CircleOfFriendsFragment());
        adapter = new MyPagerAdapter(getChildFragmentManager());
        viewPager.setAdapter(adapter);
        tabLayout.setViewPager(viewPager);
        viewPager.setOffscreenPageLimit(1);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 1){
                    picture.setVisibility(View.VISIBLE);
                }else {
                    picture.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
         picture.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        startActivity(SendCircleActivity.class);
    }


    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }
}
